package com.AAL.DAO;

import java.util.ArrayList;
import java.util.List;

import org.hibernate.HibernateException;
import org.hibernate.classic.Session;

import com.AAL.DAO.Interface.IProductInStockDAO;
import com.AAL.Model.ProductsInStock;
import com.AAL.Model.RequestingDetail;
import com.AAL.Model.WareHouse;
import com.AAL.Utils.HibernateUtil;

public class ProductInStockDAO implements IProductInStockDAO {

	@Override
	public ProductsInStock saveAndUpdateProductsInStock(
			ProductsInStock productsInStock) {
		Session session = HibernateUtil.getSessionFactory().getCurrentSession();
		session.beginTransaction();
		try {
			session.saveOrUpdate(productsInStock);
		} catch (Exception e) {
			return null;
		}
		session.getTransaction().commit();
		return productsInStock;
	}

	@Override
	public ProductsInStock deleteProductsInStock(int productInStockID) {
		Session session = HibernateUtil.getSessionFactory().getCurrentSession();
		try {
			session.beginTransaction();
			ProductsInStock productsInStock = (ProductsInStock) session.load(
					ProductsInStock.class, productInStockID);
			if (null != productsInStock) {
				session.delete(productsInStock);
			}
			session.getTransaction().commit();
			return productsInStock;
		} catch (Exception e) {
			e.printStackTrace();
			session.getTransaction().rollback();
			return null;
		}
	}

	@Override
	public ProductsInStock getProductsInStock(int productInStockID) {
		Session session = HibernateUtil.getSessionFactory().getCurrentSession();
		session.beginTransaction();
		ProductsInStock productsInStock = (ProductsInStock) session.load(
				ProductsInStock.class, productInStockID);
		return productsInStock;
	}

	@Override
	public List<ProductsInStock> getLstProductsInStocksByWarehouse(
			WareHouse wareHouse) {
		// TODO Auto-generated method stub
		return null;
	}

	// phat		

	@SuppressWarnings("unchecked")
	@Override
	public List<ProductsInStock> getLstProductsInStocksByWareHouseID(
			short wareHouseID) {
		// TODO Auto-generated method stub
		  Session session = HibernateUtil.getSessionFactory().getCurrentSession();

		  List<ProductsInStock> productInStock = null;
		  try {
		   session.beginTransaction();
		   productInStock = (List<ProductsInStock>) session
		     .createQuery("from ProductsInStock where wareHous.wareHouseID=:wareHouseID").setParameter("wareHouseID",wareHouseID).list();

		   session.getTransaction().commit();
		  } catch (HibernateException e) {
		   e.printStackTrace();
		   session.getTransaction().rollback();
		  }

		  return productInStock;
	}
	//phat end
//Huyen
	@Override
	public ProductsInStock getProductsInStockByWheelDetailIDWareHouseID(
			String WheelDetailID, short WareHouseID) {
		// TODO Auto-generated method stub
		try {
			/*Session session = HibernateUtil.getSessionFactory().getCurrentSession();
			session.beginTransaction();
			ProductsInStock productsInStock = (ProductsInStock) session.load(
					WheelDetail.class, WheelDetailID);
			return productsInStock;*/
			
			Session session = HibernateUtil.getSessionFactory().getCurrentSession();
			session.beginTransaction();
			ProductsInStock productsInStock = null;		
		
			productsInStock = (ProductsInStock) session.createQuery(
						"from ProductsInStock where wheelDetail.wheelDetailID=:wheelDetailID AND wareHous.wareHouseID=:wareHouseID")
						.setParameter("wheelDetailID",WheelDetailID).setParameter("wareHouseID", WareHouseID).uniqueResult()	;	
			
			session.getTransaction().commit();
			return productsInStock;
			
		} catch (Exception e) {
			System.out.println("Loi ham getProductsInStockByWheelDetailIDWareHouseID:"+e.getMessage());
			return null;
		}
	}
	//Phat end
	
}
